<template>
  <div class="import">
    <el-card style="text-align: center">
      <h3>{{ name }}导入</h3>
      <ReadExcel
        :beforeUpload="beforeUpload"
        :onSuccess="onSuccess"
      ></ReadExcel>
    </el-card>
  </div>
</template>

<script>
import { sysUserBatch } from "@/api/employees.js";

export default {
  data() {
    return {
      name: this.$route.query.name,
    };
  },
  methods: {
    beforeUpload(file) {
      return true;
    },
    async onSuccess({ header, results }) {
      const obj = {
        手机号: "mobile",
        姓名: "username",
        入职日期: "timeOfEntry",
        转正日期: "correctonTime",
        工号: "workNumber",
      };
      const newArr = results.map((item) => {
        const temp = {};
        Object.keys(item).forEach((item2) => {
          temp[obj[item2]] = item[item2];
          if (obj[item2] == "timeOfEntry" || obj[item2] == "correctionTime") {
            const t1 = new Date(
              (item[item2] - 1) * 24 * 60 * 60 * 1000 - 8 * 60 * 60 * 1000
            );

            t1.setYear(t1.getFullYear() - 70);

            temp[obj[item2]] = t1;
          }
        });
        return temp;
      });

      await sysUserBatch(newArr);
      this.$$message.success("导入成功");
      this.$router.push(this.$route.query.redirect || "/employees");
    },
  },
};
</script>

<style></style>
